﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BLL;
using Model;
using Common;


namespace API.Controllers
{
    [RoutePrefix("api/UserVip")]
    public class UserVipController : ApiController
    {

        B_UserVip b;
        public UserVipController()
        {
            b = new B_UserVip();
        }
        /// <summary>
        /// 获取所有会员用户数据
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpGet]
        [Route("GetData")]
        public IHttpActionResult GetData()
        {
            var data = from u in b.GetAll()
                       select new UserVip
                       {
                           VipID = u.VipID,
                           VInTime = u.VInTime,
                           VOutTime = u.VOutTime,
                           LevelID = u.LevelID,
                       };
            return Ok(new Result<List<UserVip>> { Data = data.ToList() });
        }
        /// <summary>
        /// 添加vip用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Add")]
        public IHttpActionResult Add(UserVip user)
        {
            if (b.GetAll().Count(x => x.LevelID == user.LevelID) == 0)
            {
                var data = new UserVip
                {
                    VInTime = user.VInTime,
                    VOutTime = user.VOutTime,
                    LevelID = user.LevelID,
                };
                b.Add(data);
                return Ok(new Result<UserVip> { });
            }
            else
            {
                return Ok(new Result<object>
                {
                    Code = 403,
                    Msg = "用户已存在"
                });
            }
        }


        /// <summary>
        /// 删除vip用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Delete/{id}")]
        public IHttpActionResult Delete(int id)
        {
            try
            {
                b.Delete(id);
                return Ok(new Result<object>
                {
                    Code = 200,
                    Msg = "删除成功"

                });
            }
            catch (Exception ex)
            {

                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = "该用户已删除" + ex.Message
                });
            }
        }


        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Find/{id}")]
        public IHttpActionResult Find(int id)
        {
            try
            {
                var data = b.FindById(id);
                if (data == null)
                {
                    return Ok(new Result<object>
                    {
                        Code = 404,
                        Msg = "数据丢了"
                    });
                }
                return Ok(new Result<UserVip>
                {
                    Data = new UserVip
                    {
                        VipID = data.VipID,
                        VInTime = data.VInTime,
                        VOutTime = data.VOutTime,
                        LevelID = data.LevelID

                    }
                });
            }
            catch (Exception ex)
            {

                return Ok(new Result<object>
                {
                    Data = 500,
                    Msg = ex.Message
                });
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="id"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Update/{id}")]
        public IHttpActionResult Update(int id, UserVip user)
        {
            UserVip u = new UserVip
            {
                VInTime = user.VInTime,
                VOutTime = user.VOutTime,
                LevelID = user.LevelID
            };
            try
            {
                b.Update(id, user);
                return Ok(new Result<object>());
            }
            catch (Exception)
            {

                return Ok(new Result<object>
                {
                    Data = 500,
                    Msg = "更新失败"
                });
            }
        }
    }

}
